package transport;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * ListenerAgent class support protocols factory.
 */
public final class ListenerAgent extends BaseAgent {
    private final ServerSocket serverSocket;
    public ListenerAgent(Protocol protocol, int port) throws IOException {
        super(protocol);

        serverSocket = new ServerSocket(port);
    }

    @Override
    protected void update() throws IOException {
        Socket socket = serverSocket.accept();

        try {
            Agent agent = protocol.createProtocolAgent(socket);
            agent.start();
        } catch (IOException e) {
            e.printStackTrace();

            socket.close();
        }
    }
}
